<?php
declare(strict_types=1);



use ErrorException;
use PDO;
use PDOException;
use PDOStatement;
use Swoole\Coroutine\Redis;
use Tao\src\util\RedisPool;
use WebApp\lib\Config;

/**
 * Class Table
 * @package txz\common\third_lib\model\lib
 */
class HashTable extends AbstractTable
{
    /**
     * @param $dbName
     * @param $tableName
     * @param $pre
     * @param $suffix
     * @return mixed
     */
    public function process($dbName, $tableName, $pre = '', $suffix = ''): void
    {
        $this->setPre($pre);
        $this->setSuffix($suffix);
        $this->setDbName($dbName);
        $this->setTableName(($pre ? $pre . '_' : '') . $tableName . ($suffix ? '_' . $suffix : ''));
    }

    public function init($dbName, $tableName, $pre = '', $suffix = '', $connect = false)
    {
        parent::init($dbName, $tableName, $pre, $suffix, $connect);
    }

    /**
     * @param $key
     * @return mixed
     */
    function buildConnection($key)
    {
        // TODO: Implement buildConnection() method.
    }

    /**
     * @return mixed
     */
    function getCon()
    {
        // TODO: Implement getCon() method.
    }

    /**
     * @param string $key
     * @return mixed
     */
    public function get(string $key)
    {
        return RedisPool::exec(function(Redis $client) use($key) {
            return $client->get($this->getDbName().'.'.$this->getTableName().'.'.$key);
        });
    }

    /**
     * @return mixed
     */
    public function hGetAll()
    {
        return RedisPool::exec(function(Redis $client) {
            return $client->hGetAll($this->getDbName().'.'.$this->getTableName());
        });
    }

    /**
     * @return mixed
     */
    public function del()
    {
        return RedisPool::exec(function(Redis $client) {
            return $client->del($this->getDbName().'.'.$this->getTableName());
        });
    }

    /**
     * @param string $key
     * @param string $data
     */
    public function set(string $key, string $data){
        RedisPool::exec(function(Redis $client) use($key, $data) {
            $client->set($this->getDbName().'.'.$this->getTableName().'.'.$key, $data);
        });
    }

    /**
     * @param string $key
     * @param string $data
     * @param int $time
     */
    public function setEx(string $key, string $data, int $time){
        RedisPool::exec(function(Redis $client) use($key, $data, $time) {
            $client->setex($this->getDbName().'.'.$this->getTableName().'.'.$key, $time, $data);
        });
    }

    /**
     * @param string $key
     * @return mixed
     */
    public function hGet(string $key){
        return RedisPool::exec(function(Redis $client) use($key) {
            return $client->hGet($this->getDbName().'.'.$this->getTableName(), $key);
        });
    }

    /**
     * @param string $key
     * @param string $data
     */
    public function hSet(string $key, string $data){
        RedisPool::exec(function(Redis $client) use($key, $data) {
            $client->hSet($this->getDbName().'.'.$this->getTableName(), $key, $data);
        });
    }

    /**
     * @param string[] ...$key
     * @return mixed
     */
    public function hDel(string ...$key){
        return RedisPool::exec(function(Redis $client) use($key) {
            return $client->hDel($this->getDbName().'.'.$this->getTableName(), ...$key);
        });
    }
}